package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.extend.utils.YSBeanUtil;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.Company;
import com.ys.web.entity.Quadrant;
import com.ys.web.pojo.IndustryMsgInfo;
import com.ys.web.remotemodel.RemoteIndustryInfo;
import com.ys.web.service.QuadrantService;
import com.ys.web.ws.service.ManageIndustryService;

/**
 * 四象限
 * @author wdj
 *
 */
@Service
@Transactional
public class QuadrantServiceImpl implements QuadrantService {
	
	@Autowired
	private CommonDao commonDao;
	@Resource
	private ManageIndustryService manageIndustryService;
	
	/**
	 * 根据行业id查找所属象限信息
	 * @param industryId 行业id
	 * @return 所属象限信息列表
	 */
	@Override
	public List<IndustryMsgInfo> getQuadrantByIndustryId(String industryId) {
		List<IndustryMsgInfo> listInfo = new ArrayList<>();
		try {
			List<Quadrant> list = commonDao.findByProperty(Quadrant.class, "industryId", industryId);
			if(list==null || list.isEmpty()) {
				for (int i = 0; i < 4; i++) {
					IndustryMsgInfo quadrantInfo = new IndustryMsgInfo();
					quadrantInfo.setIndustryId(industryId);
					quadrantInfo.setConsumeRateMin(0.00);
					quadrantInfo.setConsumeRateMax(0.00);				
					listInfo.add(quadrantInfo);				
				}
			}
			else{
				for (Quadrant quadrant : list) {
					IndustryMsgInfo quadrantInfo = new IndustryMsgInfo();
					YSBeanUtil.copyBeanNotNull2BeanExactly(quadrant, quadrantInfo);
					quadrantInfo.setQuadrantId(quadrant.getId());
					listInfo.add(quadrantInfo);
				}			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listInfo;
	}
	
	/**
	 * 批量更新属象限信息
	 * @param quadrantInfoList
	 */
	public void batchUpdateQuadrantInfo(IndustryMsgInfo[] quadrantInfoList){
		for (IndustryMsgInfo quadrantInfo : quadrantInfoList) {
			Quadrant quadrant = new Quadrant();
			
			try {
				YSBeanUtil.copyBeanNotNull2BeanExactly(quadrantInfo, quadrant);
				if(quadrantInfo.getQuadrantId()!=null) {
					quadrant.setId(quadrantInfo.getQuadrantId());
				}
				
				commonDao.saveOrUpdate(quadrant);			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 根据公司id查找行业所属的四象限边界值
	 * @param companyId 公司id
	 * @return 行业所属的四象限信息
	 */
	@Override
	public List<IndustryMsgInfo> getQuadrantByCompanyId(Integer companyId) {
		List<IndustryMsgInfo> listInfo = new ArrayList<>();
		
		try {
			RemoteIndustryInfo RemoteIndustryInfo = manageIndustryService.getIndustryByCompanyId(companyId);
			List<Quadrant> list = commonDao.findByProperty(Quadrant.class, "industryId", RemoteIndustryInfo.getId());
			if(list==null || list.isEmpty()) {
				for (int i = 0; i < 4; i++) {
					IndustryMsgInfo quadrantInfo = new IndustryMsgInfo();
					quadrantInfo.setIndustryId(RemoteIndustryInfo.getId());
					quadrantInfo.setConsumeRateMin(0.00);
					quadrantInfo.setConsumeRateMax(0.00);				
					listInfo.add(quadrantInfo);				
				}
			}
			else{
				for (Quadrant quadrant : list) {
					IndustryMsgInfo quadrantInfo = new IndustryMsgInfo();
					YSBeanUtil.copyBeanNotNull2BeanExactly(quadrant, quadrantInfo);
					quadrantInfo.setQuadrantId(quadrant.getId());
					listInfo.add(quadrantInfo);
				}		
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return listInfo;
	}
}
